package com.tracy.lifeproject.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gc.flashview.FlashView;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.tracy.lifeproject.R;
import com.tracy.lifeproject.adapter.Description_VpAdapter;
import com.tracy.lifeproject.bean.DescriptionBean;
import com.tracy.lifeproject.constant.UrlString;
import com.tracy.lifeproject.discovery.goods.activity.BuyerRecordActivity;
import com.tracy.lifeproject.fragment.CommentFragment;
import com.tracy.lifeproject.fragment.WebFragment;
import com.tracy.lifeproject.utils.CircleImageView;
import com.tracy.lifeproject.utils.OKHttpUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class DescriptionActivity extends AppCompatActivity {

    @InjectView(R.id.iv_share)
    ImageView mIvShare;
    @InjectView(R.id.fv_pic)
    FlashView mFvPic;
    @InjectView(R.id.tv_title)
    TextView mTvTitle;
    @InjectView(R.id.tv_price)
    TextView mTvPrice;
    @InjectView(R.id.tv_desc)
    TextView mTvDesc;
    @InjectView(R.id.ll_container)
    LinearLayout mLlContainer;
    @InjectView(R.id.ll_piccontainer)
    LinearLayout mLlPiccontainer;
    @InjectView(R.id.tv_favorite)
    TextView mTvFavorite;
    @InjectView(R.id.tab)
    TabLayout mTab;
    @InjectView(R.id.vp_bottom)
    ViewPager mVpBottom;
    @InjectView(R.id.tb_desc)
    Toolbar mTbDesc;
    @InjectView(R.id.btn_taobao)
    Button mBtnTaobao;
    @InjectView(R.id.tv_back)
    TextView mTvBack;
    @InjectView(R.id.ll_buyerRecord)
    LinearLayout llBuyerRecord;


    private String mDescriptionUrl;
    private String tag = "tag";
    private List<Fragment> mList;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_description);
        ButterKnife.inject(this);

        Intent intent = getIntent();
        id = intent.getIntExtra("id", 0);
        mDescriptionUrl = UrlString.getDescriptionUrl(id);

        mTbDesc.getBackground().setAlpha(255);

        initData();
    }

    private void initData() {
        mList = new ArrayList<>();
        OKHttpUtils.newInstance(this).getAsyncMainData(mDescriptionUrl, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {
                Toast.makeText(DescriptionActivity.this, "网络错误，加载失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String result) {
                DescriptionBean descriptionBean = new Gson().fromJson(result, DescriptionBean.class);
                initView(descriptionBean);
            }
        }, tag);
    }

    private void initView(DescriptionBean descriptionBean) {
        final DescriptionBean.DataBean dataBean = descriptionBean.getData();
        mFvPic.setImageUris(dataBean.getImage_urls());

        mTvTitle.setText(dataBean.getName());

        mTvPrice.setText("￥" + dataBean.getPrice());

        mTvDesc.setText(dataBean.getDescription());

        mTvFavorite.setText(dataBean.getPublic_favorites_count() + "个清单收录");

        mList.add(WebFragment.newInstance(dataBean.getDetail_html()));
        mList.add(new CommentFragment());
        mVpBottom.setAdapter(new Description_VpAdapter(getSupportFragmentManager(), mList));

        mTab.setupWithViewPager(mVpBottom);
        mTab.getTabAt(0).setText("图文介绍");
        mTab.getTabAt(1).setText("评论（0）");

        //添加中间的广告
        for (int i = 0; i < dataBean.getRelated_posts().size(); i++) {
            View itemView = LayoutInflater.from(this).inflate(R.layout.description_strip, null);
            TextView tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            final DescriptionBean.DataBean.RelatedPostsBean relatedPostsBean = dataBean.getRelated_posts().get(i);
            tv_name.setText(relatedPostsBean.getTitle());
            //单条记录点击后的跳转
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DescriptionActivity.this, DetailActivity.class);
                    intent.putExtra("id", relatedPostsBean.getId());
                    startActivity(intent);
                }
            });
            mLlContainer.addView(itemView);
        }

        //添加中间的清单收录上方的原图
        for (int i = 0; i < dataBean.getRelated_favorites_avarta().size(); i++) {
            CircleImageView civ = new CircleImageView(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(30, 30);
            params.setMargins(0, 4, 4, 0);
            civ.setLayoutParams(params);
            Picasso.with(this).load(dataBean.getRelated_favorites_avarta().get(i)).into(civ);
            mLlPiccontainer.addView(civ);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        OKHttpUtils.newInstance(this).cancel(tag);
    }

    @OnClick({R.id.tv_back, R.id.iv_share, R.id.btn_taobao,R.id.ll_buyerRecord, R.id.iv_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.iv_share:

                break;
            case R.id.btn_taobao:
                //TODO
                break;
            case R.id.ll_buyerRecord:
                Intent intent = new Intent(this,BuyerRecordActivity.class);
                intent.putExtra("id",id);
                startActivity(intent);
                break;
        }
    }
}
